package com.sihai.ehr.service.impl;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.ExamQualStandardSet;
import com.sihai.ehr.service.ExamQualStandSetService;

/**
 * 定性考核标准设置服务实现类
 * 
 * @author Henry
 * 
 */
@Service("examQualStandSetService")
public class ExamQualStandSetServiceImpl implements ExamQualStandSetService {
	@Resource
	DataAccessor dataAccessor;

	@Override
	public boolean checkScoreAreaExist(final Long organizationId,
			final Float scoreStart, final Float scoreEnd, final Long id) {
		Long existCount = 0L;
		try {
			existCount = dataAccessor.findUnique(Long.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							// TODO Auto-generated method stub
							if (id > 0) {
								return session
										.createQuery("select count(e.id) from ExamQualStandardSet e where e.cancel<>1 and e.organization.id="
												+ organizationId
												+ " and (e.scoreStart <="
												+ scoreStart
												+ " and e.scoreEnd >="
												+ scoreStart
												+ " or e.scoreStart <="
												+ scoreEnd
												+ " and e.scoreEnd >="
												+ scoreEnd
												+ ") and e.id <> "
												+ id);
							} else {
								return session
										.createQuery("select count(e.id) from ExamQualStandardSet e where e.cancel<>1 and e.organization.id="
												+ organizationId
												+ " and (e.scoreStart <="
												+ scoreStart
												+ " and e.scoreEnd >="
												+ scoreStart
												+ " or e.scoreStart <="
												+ scoreEnd
												+ " and e.scoreEnd >="
												+ scoreEnd + ")");
							}

						}
					});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (existCount > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public ExamQualStandardSet getExamQualStandardSet(Long id) {
		if (id == null || id < 0) {
			throw new RuntimeException("【传入参数非法】【发生地点:" + this.getClass() + "】");
		}
		try {
			return dataAccessor.get(ExamQualStandardSet.class, id);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取定性考核标准对象数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：ExamQualStandSetServiceImpl.getExamQualStandardSet;参数id="
							+ id + " 】");
		}
	}

	@Override
	public LimitedList<ExamQualStandardSet> searchExamQualStandardSets(
			final Long organizationId, Limit limit, Sorter sorter) {
		if (organizationId <= 0) {
			throw new RuntimeException("【传入参数非法】【organizationId:"
					+ organizationId + "】【发生地点:" + this.getClass() + "】");
		}
		try {
			return dataAccessor.findByQuery(ExamQualStandardSet.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from ExamQualStandardSet e where e.cancel <> 1");
							dynamicQuery.appendIfNotNull(
									"and e.organization.id =:organization_id",
									"organization_id", organizationId);
							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取强制分布设置集合时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：ExamQualStandSetServiceImpl.searchExamQualCoerceSets;参数organizationId="
							+ organizationId + "】");
		}
	}

	@Override
	public LimitedList<ExamQualStandardSet> searchExamQualStandardSets(
			final ExamQualStandardSet examQualStandardSet, Limit limit,
			Sorter sorter) {
		if (examQualStandardSet == null) {
			try {
				return dataAccessor.findByClass(ExamQualStandardSet.class,
						limit, sorter);
			} catch (Exception e) {
				throw new RuntimeException(
						"【异常原因：根据定性标准设置访问数据库发生异常】【发生地点：ExamQualStandSetServiceImpl.searchExamQualStandardSets;参数ExamQualStandardSet为空】");
			}
		}
		try {
			return dataAccessor.findByQuery(ExamQualStandardSet.class,
					new QueryCreator() {
						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from ExamQualStandardSet e where e.cancel <> 1");
							dynamicQuery.appendIfNotNull(
									"and e.organization.id =:organization_id",
									"organization_id", examQualStandardSet
											.getOrganization().getId());
							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：根据定性标准设置访问数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：ExamQualStandSetServiceImpl.searchExamQualCoerceSets;参数examQualCoerceSet="
							+ examQualStandardSet.toString() + "】");
		}
	}

	@Override
	public void saveExamQualStandardSet(ExamQualStandardSet examQualStandardSet) {
		if (examQualStandardSet == null
				|| examQualStandardSet.getOrganization() == null) {
			throw new RuntimeException("【examQualStandardSet对象参数异常】");
		}
		try {
			dataAccessor.saveOrUpdate(examQualStandardSet);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：保存定性考核标准设置对象到数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：ExamQualStandSetServiceImpl.saveExamQualStandardSet】");
		}
	}

	@Override
	public void deleteExamQualStandardSetById(final Long id) {
		if (id == null && id <= 0) {

		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session
							.createQuery("delete from ExamQualStandardSet e where e.id="
									+ id);
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：移出定性考核标准设置集合操作数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：ExamQualStandSetServiceImpl.removeExamQualStandardSets】");
		}
	}

	@Override
	public void removeExamQualStandardSets(final String examQualStandardSetIds) {
		if (examQualStandardSetIds == null || examQualStandardSetIds.isEmpty()) {
			throw new RuntimeException("【参数异常：examQualStandardSetIds为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("delete from ExamQualStandardSet e where id in ({0})",
									examQualStandardSetIds));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：移出定性考核标准设置集合操作数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：ExamQualStandSetServiceImpl.removeExamQualStandardSets】");
		}
	}

}
